package com.bestv.search.spider.douban.util;

import com.bestv.search.spider.douban.model.DouBanAppKey;
import com.bestv.search.spider.util.SpiderConst;

import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class DouBanAppKeyPool implements Runnable {
    private static Lock lock = new ReentrantLock();
    private static List<DouBanAppKey> keys = Collections.synchronizedList(new ArrayList<DouBanAppKey>());
    public static boolean isBusy = false;
    static {
        for (String keyId : SpiderConst.DOUBAN_APP_KEYS) {
            DouBanAppKey appKey = new DouBanAppKey();
            appKey.setKeyId(keyId);
            appKey.setCount(0);
            appKey.setLocked(false);
            appKey.setLastAccessTime(new Date());
            keys.add(appKey);
        }

        // start one thread to manager pool of DouBanAppKey
        Thread t = new Thread(new DouBanAppKeyPool());
        t.setDaemon(true);
        t.start();
    }

    public static DouBanAppKey getAppKey() {
        try {
            lock.lock();
            List<DouBanAppKey> unLockedList = new ArrayList<DouBanAppKey>();
            for (DouBanAppKey key : keys) {
                if (!key.isLocked()) {
                    unLockedList.add(key);
                }
            }
            if (unLockedList.size() == 0) {
                isBusy = true;
                return null;
            } else {
                int index = new Random().nextInt(unLockedList.size());
                DouBanAppKey key = unLockedList.get(index);
                key.setCount(key.getCount() + 1);
                key.setLastAccessTime(new Date());
                if (key.getCount() >= SpiderConst.DOUBAN_ACCESS_LIMIT_COUNT) {
                    key.setLocked(true);
                }
                return key;
            }
        } finally {
            lock.unlock();
        }
    }

    @Override
    public void run() {
        // maintain pool
        while (true) {
            try {
                lock.lock();
                for (DouBanAppKey key : keys) {
                    if (key.isLocked()) {
                        Calendar now = Calendar.getInstance();
                        Calendar lastAccessTime = Calendar.getInstance();
                        lastAccessTime.setTime(key.getLastAccessTime());
                        lastAccessTime.add(Calendar.MINUTE, SpiderConst.DOUBAN_ACCESS_INTERVAL_MINUTES);
                        // Compare access time whether is more before than
                        // YOUKU_ACCESS_INTERVAL_MINUTES to now
                        if (lastAccessTime.getTime().before(now.getTime())) {
                            key.setLocked(false);
                            key.setCount(0);
                            isBusy = false;
                        }
                    } else {
                        isBusy = false;
                    }
                }
            } finally {
                lock.unlock();
            }

            // Wait for next times check
            try {
                Thread.sleep(SpiderConst.THREAD_CHECK_INTERVAL_SECOND * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
